import {v4 as uuidv4} from 'uuid';
import {EPoint} from "./EPoint.ts";
import {ComponentAnchor, ComponentOption} from "./ComponentOption.ts";
import {reactive,} from "vue";

export class LinkOption {
    //开始锚点，指向一个锚点，Null代表开始节点没有连接锚点
    startAnchor: ComponentAnchor | null = null;
    endAnchor: ComponentAnchor | null = null;
    id: string;
    points: Array<EPoint> = reactive([])
    selected: boolean = false;
    option: Object = {};
    //线宽度
    width: number = 3;
    //填充颜色
    fillColor: string = '#aaa';
    //边框颜色
    borderColor: string = '#FFF';
    //边框宽度
    borderWidth: number = 0
    //自动水平和垂直
    autoHV: boolean = false;
    //是否有箭头
    static hasArrow: boolean = true;
    //是否处在拖拽点的过程中
    isDraggingPoint: boolean = false;

    constructor(id: string | null | undefined) {
        if (typeof id === 'undefined' || !id) {
            this.id = uuidv4();
        }
    }

    pushXY(x: number, y: number) {
        let point = new EPoint(x, y);
        this.points.push(point)
    }

    pushPoint(point: EPoint) {
        this.points.push(point)
    }

    setLastPointObject(point: EPoint){
        let length = this.points.length;
        if (length > 1) {
            this.points[length - 1] = point;
        } else {
            this.points.push(point);
        }
    }
    setLastPoint(x: number, y: number) {
        let point = new EPoint(x, y);
        let length = this.points.length;
        if (length > 1) {
            this.points[length - 1] = point;
        } else {
            this.points.push(point);
        }
    }

    static startLink(x: number, y: number) {
        let nLink = new LinkOption(null)
        nLink.pushXY(x, y)
        return nLink;
    }

    static startLinkWithAnchor(anchor: ComponentAnchor, zoom: number) {
        let nLink = new LinkOption(null)
        let point = new EPoint(anchor.point.x / zoom, anchor.point.y / zoom);
        nLink.pushPoint(point)
        nLink.startAnchor = anchor
        return nLink;
    }

    static fromJson(linkObj: any, componentList: ComponentOption[]) {
        let linkOption = new LinkOption(linkObj.id)
        linkOption.id = linkObj.id;
        let jPoints = linkObj.points || [];
        for (let jPoint of jPoints) {
            linkOption.pushPoint(new EPoint(jPoint.x, jPoint.y))
        }
        linkOption.option = linkObj.option
        linkOption.width = linkObj.width
        linkOption.fillColor = linkObj.fillColor
        linkOption.borderColor = linkObj.borderColor
        linkOption.borderWidth = linkObj.borderWidth
        linkOption.autoHV = linkObj.autoHV
        linkOption.hasArrow = linkObj.hasArrow
        for (let co of componentList) {
            for (let anchor of co.anchors) {
                if (linkObj.startAnchorId === anchor.id) {
                    linkOption.startAnchor = anchor;
                }
                if (linkObj.endAnchorId === anchor.id) {
                    linkOption.endAnchor = anchor;
                }
            }
        }
        return linkOption;
    }

    toJson(): any {
        let linkObj: any = {}
        linkObj.id = this.id
        linkObj.points = []
        for (let p of this.points) {
            linkObj.points.push(p.toJson());
        }
        linkObj.option = this.option
        linkObj.width = this.width
        linkObj.fillColor = this.fillColor
        linkObj.borderColor = this.borderColor
        linkObj.borderWidth = this.borderWidth
        linkObj.autoHV = this.autoHV
        linkObj.hasArrow = this.hasArrow
        if (this.startAnchor) {
            linkObj.startAnchorId = this.startAnchor.id
        }
        if (this.endAnchor) {
            linkObj.endAnchorId = this.endAnchor.id
        }
        return linkObj;
    }
}


